package com.mywang.weitravel.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.mywang.weitravel.R;
import com.mywang.weitravel.model.FootprintListBean;
import com.mywang.weitravel.modules.footprint.FootPrintActivity;
import com.mywang.weitravel.util.ImageLoader;
import com.mywang.weitravel.util.Utils;

import java.util.ArrayList;
import java.util.List;

public class FootprintListAdapter extends RecyclerView.Adapter<FootprintListAdapter.FootprintHolder> {

    private Context mContext;

    private List<FootprintListBean.DataBean> mDatas = new ArrayList<>();

    private MarkListener mListener;

    public FootprintListAdapter(Context context, MarkListener listener) {
        this.mContext = context;
        this.mListener = listener;
    }

    public void addData(List<FootprintListBean.DataBean> beans) {
        mDatas.addAll(beans);
        notifyItemInserted(mDatas.size());
    }

    public void refreshData(List<FootprintListBean.DataBean> listBeans) {
        mDatas.clear();
        mDatas.addAll(listBeans);
        notifyItemRangeChanged(0, listBeans.size());
    }

    //点赞更新
    public void updateItem(int position) {
        mDatas.get(position).setIs_support(1);
        mDatas.get(position).setNumber(mDatas.get(position).getNumber() + 1);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public FootprintHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_footprint_list, parent, false);
        return new FootprintHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FootprintHolder holder, int position) {
        FootprintListBean.DataBean dataBean = mDatas.get(position);
        holder.tvTitle.setText(dataBean.getTitle());
        holder.tvDianzanNum.setText("" + dataBean.getNumber());
        ImageLoader.getInstance().displayImage(mContext, dataBean.getFilepath(), holder.ivPicture);
        if (dataBean.getIs_support() == 1) {
            holder.ivDianzan.setImageResource(R.mipmap.icon_video_dianzan_red);
        } else {
            holder.ivDianzan.setImageResource(R.mipmap.icon_video_dianzan_normal);
        }

        holder.ivPicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isEmpty(dataBean.getContent())) {
                    ToastUtils.showShort("数据异常");
                    return;
                }
                Intent intent = new Intent(mContext, FootPrintActivity.class);
                intent.putExtra("data", dataBean.getContent());
                intent.putExtra("title", dataBean.getTitle());
                mContext.startActivity(intent);
            }
        });

        holder.ivDianzan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.Mark(position, dataBean.getId() + "");
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class FootprintHolder extends RecyclerView.ViewHolder {
        ImageView ivPicture;
        TextView tvTitle;
        ImageView ivDianzan;
        TextView tvDianzanNum;

        public FootprintHolder(@NonNull View itemView) {
            super(itemView);
            ivPicture = itemView.findViewById(R.id.iv_picture);
            tvTitle = itemView.findViewById(R.id.tv_title);
            ivDianzan = itemView.findViewById(R.id.iv_dianzan);
            tvDianzanNum = itemView.findViewById(R.id.tv_dianzan_num);
        }
    }

    public interface MarkListener {
        void Mark(int position, String id);
    }
}
